<template>
  <!-- 提现管理 -->
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="mb-[10px]" v-show="showSearch">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="68px">
            <el-form-item label="提现编号" prop="sn">
              <el-input v-model.trim="queryParams.sn" placeholder="请输入" clearable style="width: 200px" />
            </el-form-item>
            <!-- <el-form-item label="批次编号" prop="batchSn">
              <el-input v-model.trim="queryParams.batchSn" placeholder="请输入" clearable style="width: 200px" />
            </el-form-item> -->
            <el-form-item label="状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="请选择" clearable style="width: 150px">
                <el-option label="待审核" value="PEND" />
                <!-- <el-option label="进行中" value="ACTIVE" /> -->
                <el-option label="审核成功" value="SUCCESS" />
                <el-option label="审核失败" value="FAIL" />
              </el-select>
            </el-form-item>
            <!-- <el-form-item v-show="1" label="店铺ID" prop="sellerId">
              <el-input v-model.trim="queryParams.sellerId" placeholder="请输入" clearable style="width: 200px" />
            </el-form-item> -->
            <el-form-item label="店铺">
              <el-select
                v-model="queryParams.sellerId"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option :label="item.name" :value="item.id" v-for="(item, index) in shopList"/>
              </el-select>
            </el-form-item>
            <el-form-item v-show="tuonsein" label="姓名" prop="realName">
              <el-input v-model.trim="queryParams.realName" placeholder="请输入" clearable style="width: 200px" />
            </el-form-item>
            <el-form-item label="时间范围" prop="timeList">
              <el-date-picker v-model="timeList" type="daterange" range-separator="-" start-placeholder="开始时间"
                end-placeholder="结束时间" format="YYYY-MM-DD" value-format="YYYY-MM-DD HH:mm:ss" :default-time="defaultTime"
                style="width: 230px" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery" icon="Search">搜索</el-button>
              <el-button @click="resetQuery" icon="Refresh">重置</el-button>
              <el-button link :icon="`${tuonsein ? 'ArrowUp' : 'ArrowDown'}`" @click="tuonsein = !tuonsein"></el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>
    <el-card shadow="hover">
      <!-- 功能按钮 -->
      <template #header>
        <el-row :gutter="10">
          <!-- <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleDownload()">导出</el-button>
          </el-col> -->
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :search="true"></right-toolbar>
        </el-row>
      </template>
      <!-- 表格 -->
      <el-table ref="supTableRef" v-loading="loading" :data="withdrawList" max-height="550" border
        @selection-change="handleSelectionChange">
        <el-table-column label="店铺" prop="sellerId" align="center" width="180">
          <template #default="scope">
            <div class="lioneuo">
              <el-row>
                <!-- <el-col :span="5" class="cuionseti">ID：</el-col> -->
                <el-col :span="19">{{ scope.row.sellerName || '--' }}</el-col>
              </el-row>
              <!-- <el-row>
                <el-col :span="5" class="cuionseti">编号：</el-col>
                <el-col :span="19">{{ scope.row.userSn || '--' }}</el-col>
              </el-row> -->
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column label="提现类型" prop="type" align="center">
          <template #default="scope">{{ typeFilters(scope.row.type) }}</template>
        </el-table-column> -->
        <el-table-column label="银行名称" prop="bankName" align="center" width="120" />
        <el-table-column label="银行账号" prop="bankCardNo" align="center" width="175" />
        <el-table-column label="姓名" prop="realName" align="center" width="70" />
        <el-table-column label="金额/元" prop="amount" align="center" width="85" />
        <el-table-column label="手续费/元" prop="fee" align="center" width="85" />
        <el-table-column label="到账金额/元" prop="realAmount" align="center" width="95" />
        <el-table-column label="提现状态" prop="status" align="center" width="85">
          <template #default="scope">{{ statusFilters(scope.row.status) }}</template>
        </el-table-column>
        <el-table-column label="提现编号" prop="sellerId" align="center" width="250">
          <template #default="scope">
            <div class="lioneuo">
              <el-row>
                <!-- <el-col :span="7" class="cuionseti">提现：</el-col> -->
                <el-col :span="24">{{ scope.row.sn }}</el-col>
              </el-row>
              <!-- <el-row>
                <el-col :span="7" class="cuionseti">批次：</el-col>
                <el-col :span="17">{{ scope.row.batchSn }}</el-col>
              </el-row>
              <el-row>
                <el-col :span="7" class="cuionseti">三方提现：</el-col>
                <el-col :span="17">{{ scope.row.outSn }}</el-col>
              </el-row> -->
            </div>
          </template>
        </el-table-column>
        <el-table-column label="备注" prop="remark" align="center" min-width="150" />
        <el-table-column label="创建时间" prop="createTime" align="center" width="140">
          <template #default="scope">{{ scope.row.createTime.slice(0, -3) }}</template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="120">
          <template #default="scope">
            <!-- <el-tooltip :content="`${scope.row.status === 'PEND' ? '' : '已'}审核`" placement="top">
              <el-button link type="primary" icon="Open" :disabled="scope.row.status !== 'PEND'"
                @click="approve(scope.row)"></el-button>
            </el-tooltip> -->
            <!-- <span 
              style="font-weight: bold;"
              :style="{color: scope.row.status=='PEND' ? '#ce8f59' : scope.row.status=='SUCCESS' ? '#1bd66c' : scope.row.status=='FAIL' ? '#1e1e1e' : ''}"
            >
              {{ scope.row.status=='PEND' ? '待审核' : scope.row.status=='SUCCESS' ? '审核成功' : scope.row.status=='FAIL' ? '审核失败' : '--' }}
            </span> -->
            <el-button v-if="scope.row.status=='PEND'" @click="approve(scope.row)" type="primary">审核</el-button>
            <span v-else>--</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="auditRef" :model="auditinfo" label-width="80px">
        <el-form-item label="提现状态" prop="audit">
          <el-radio-group v-model="auditinfo.audit">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="0">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="auditinfo.remark" :rows="2" type="textarea" placeholder="请输入……" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="Withdraw">
import { listWithdraw, auditWithdraw } from '@/api/operates/withdraw/index';
import { WithdrawQuery, auditForm, WithdrawVO } from '@/api/operates/withdraw/types';
import { listPage as shopListApi } from '@/api/operates/shopList/index'
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const tuonsein = ref(false)
const upIcon = ref('')
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const timeList = ref<any>([])
const shopList = ref<any>([])
const ids = ref<Array<string | number>>([])

const queryFormRef = ref<ElFormInstance>();
const auditRef = ref<ElFormInstance>();
// 表格数据
const withdrawList = ref<WithdrawVO[]>([]);

// 默认时间
const defaultTime = ref<[Date, Date]>([
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59),
])
// 查询表单
const queryParams = ref<WithdrawQuery>({
  pageNum: 1,
  pageSize: 10,
});
// 审核表单
const auditinfo = ref<auditForm>({
  id: undefined,
  audit: 0,
  remark: undefined,
})
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

// 获取商家数据
function getShopList(){
  shopListApi({pageNum:1, pageSize:100000}).then((res)=>{
    shopList.value = res.data.list
  })
}

/**
 * 查询商品列表
 */
const getList = async () => {
  loading.value = true
  const query = { ...queryParams.value }
  if (timeList.value.length) {
    query.startTime = timeList.value[0]
    query.endTime = timeList.value[1]
  }
  const res = await listWithdraw(query);
  withdrawList.value = res.data.list;
  total.value = res.data.total;
  loading.value = false;
}

/**
 * 搜索按钮操作
 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/**
 * 重置搜索
 */
const resetQuery = () => {
  timeList.value = [];
  queryFormRef.value?.resetFields();
  handleQuery();
}

/**
 * 导出
 */
const handleDownload = () => {
  const queryExport = { ...queryParams.value }
  if (timeList.value.length) {
    queryExport.startTime = timeList.value[0]
    queryExport.endTime = timeList.value[1]
  }
  proxy?.download('/withdraw/export', {
    ...queryExport
  }, `提现记录.xlsx`)
}

/**
 * 多选框选中数据
 * @param selection 
 */
const handleSelectionChange = (selection: WithdrawVO[]) => {
  ids.value = selection.map((item: WithdrawVO) => item.id);
}

/**
 * 过滤（提现类型）
 * @param val 
 */
const typeFilters = (val: string) => {
  if (val === 'BANK') {
    return '银行卡'
  }
}

/**
 * 过滤（提现状态）
 * @param val 
 */
const statusFilters = (val: string) => {
  if (val === 'PEND') {
    return '待审核'
  } else if (val === 'ACTIVE') {
    return '--'
  } else if (val === 'SUCCESS') {
    return '审核成功'
  } else if (val === 'FAIL') {
    return '审核失败'
  }
}

// 重置
const reset = () => {
  auditRef.value?.resetFields();
}
/**
 * 审核
 * @param row 
 */
const approve = async (row: WithdrawVO) => {
  reset();
  auditinfo.value.id = row.id;
  auditinfo.value.audit = 0;
  dialog.visible = true;
  dialog.title = "提现审核";
}
// 提交
const submitForm = async () => {
  try {
    await auditWithdraw(auditinfo.value);
    proxy?.$modal.msgSuccess('审核已提交');
    dialog.visible = false;
    getList();
  } catch (error) {

  }
}
// 取消
const cancel = () => {
  reset();
  dialog.visible = false;
}

onMounted(() => {
  getShopList()
  getList();
});

</script>

<style scoped></style>